package jlxy.ise.eroad.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;
import jlxy.ise.eroad.R;
import jlxy.ise.eroad.activity.OrderDetailActivity;
import jlxy.ise.eroad.adapter.OrderAdapter;
import jlxy.ise.eroad.anim.CustomItemAnimator;
import jlxy.ise.eroad.base.BaseSwitchFragment;
import jlxy.ise.eroad.bean.PackageInfo;
import jlxy.ise.eroad.bean.ServerRpsInfo;
import jlxy.ise.eroad.util.SingleToast;
import jlxy.ise.eroad.util.WebUtil;


/**
 * 我完成的订单页面
 *
 * @author chen
 */
public class MyOrderFragment extends BaseSwitchFragment implements OrderAdapter.OnItemClick {

    private static final String TAG = MyOrderFragment.class.getSimpleName();

    private View rootView;
    private OrderAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private TextView mTxTitle;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private static MyOrderFragment instance;

    public MyOrderFragment() {
    }

    public static MyOrderFragment getInstance() {
        if (instance == null) instance = new MyOrderFragment();
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_order_list, container, false);
        initViews();
        return rootView;
    }

    private void initViews() {
        mTxTitle = (TextView) rootView.findViewById(R.id.fragment_title);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mAct));
        mRecyclerView.setItemAnimator(new CustomItemAnimator());
        mAdapter = new OrderAdapter(mApp.getMyCompletedPkgList(), mAct, this);
        mRecyclerView.setAdapter(mAdapter);

        mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);

        //下拉刷新
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doRefresh();
            }
        });
        mSwipeRefreshLayout.setRefreshing(true);
        doRefresh();

        mTxTitle.setText(R.string.menu_my_order);
    }

    /**
     * 获取服务器的消息数据
     */
    private void doRefresh() {


        //修改密码
        WebUtil.postCompletedList(WebUtil.getRpCompletedList(mApp.getDriverInfo().getId() + ""), new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Gson gson = new Gson();
                try {
                    ServerRpsInfo serverRpsInfo = gson.fromJson(response.toString(), ServerRpsInfo.class);
                    if (serverRpsInfo.getStatus().equals("ok") && serverRpsInfo.getMessage().equals("ok")) {
                        //说明获取成功
                        PackageInfo packageInfo = gson.fromJson(response.toString(), PackageInfo.class);
                        mApp.getMyCompletedPkgList().clear();
                        mApp.getMyCompletedPkgList().addAll(packageInfo.getData());
                        mAdapter.notifyDataSetChanged();
                        //此次刷新结束
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                } catch (Exception e) {
                    SingleToast.showToast(mAct, "获取数据出错:" + e.getLocalizedMessage(), 2000);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
                SingleToast.showToast(mAct, "修改数据失败", 2000);
            }
        });
    }


    @Override
    public void OnClickItem(PackageInfo.DataBean mInfo) {
        OrderDetailActivity.sPackageInfo = mInfo;
        OrderDetailActivity.sType = 0;
        Intent intent = new Intent(mAct, OrderDetailActivity.class);
        startActivity(intent);
    }

    @Override
    public void OnLongClickItem(PackageInfo.DataBean mInfo) {

    }
}
